/**
 * qq工具类
 * Created by zhangluxin on 2017/2/8.
 */
var QQUtil = {};

QQUtil.registerAppByAppId = function (app_id) {
    if (cc.sys.os === cc.sys.OS_IOS) {
        jsb.reflection.callStaticMethod('QQUtil', 'registerAppByAppId:', app_id);
    } else if (cc.sys.os == cc.sys.OS_ANDROID) {
    }
};

/**
 * 请求qq登录
 */
QQUtil.redirectToQQLogin = function () {
    if (cc.sys.os == cc.sys.OS_IOS) {
        jsb.reflection.callStaticMethod('QQUtil', 'redirectToQQLogin');
    } else if (cc.sys.os == cc.sys.OS_ANDROID) {
        jsb.reflection.callStaticMethod(packageUri + '/utils/QQUtil', 'redirectToQQLogin', '()V');
    }
};

/**
 * 获取qq用户信息
 * @returns {String} 用户信息
 */
QQUtil.getQQUserInfo = function () {
    if (cc.sys.os == cc.sys.OS_IOS) {
        return jsb.reflection.callStaticMethod('QQUtil', 'getQQUserInfo');
    } else if (cc.sys.os == cc.sys.OS_ANDROID) {
        return jsb.reflection.callStaticMethod(packageUri + '/utils/QQUtil', 'getQQUserInfo', '()Ljava/lang/String;');
    }
};

/**
 * 获取qqToken
 * @returns {String} qqToken
 */
QQUtil.getQQToken = function () {
    if (cc.sys.os == cc.sys.OS_IOS) {
        return jsb.reflection.callStaticMethod('QQUtil', 'getQQToken');
    } else if (cc.sys.os == cc.sys.OS_ANDROID) {
        return jsb.reflection.callStaticMethod(packageUri + '/utils/QQUtil', 'getQQToken', '()I');
    }
};

/**
 * 获取qqOpenID
 * @returns {String} qqOpenID
 */
QQUtil.getQQOpenID = function () {
    if (cc.sys.os == cc.sys.OS_IOS) {
        return jsb.reflection.callStaticMethod('QQUtil', 'getQQOpenID');
    } else if (cc.sys.os == cc.sys.OS_ANDROID) {
        return jsb.reflection.callStaticMethod(packageUri + '/utils/QQUtil', 'getQQOpenID', '()I');
    }
};

/**
 * 分享图片到qq
 * @param node 截图的node
 */
QQUtil.sharePic = function (node) {
    var winSize = cc.director.getWinSize();
    var texture = new cc.RenderTexture(winSize.width, winSize.height, null, 0x88F0);
    if (!texture)
        return;

    texture.retain();
    texture.setAnchorPoint(0, 0);
    texture.begin();
    node.visit();
    texture.end();
    var time = timestamp2time(Math.round((new Date()).valueOf() / 1000));
    time = time.replace(/[\s:-]+/g, '_');
    var namePNG = 'ss-' + time + '.png';
    var nameJPG = 'ss-' + time + '.jpg';

    if (cc.sys.os == cc.sys.OS_IOS) {
        texture.saveToFile(namePNG, cc.IMAGE_FORMAT_PNG, true, function () {
            texture.release();
            jsb.reflection.callStaticMethod(
                'QQUtil',
                'sharePic:imageName:sceneType:',
                jsb.fileUtils.getWritablePath(),
                namePNG,
                0
            );
        });

    } else if (cc.sys.os == cc.sys.OS_ANDROID) {
        texture.saveToFile(nameJPG, cc.IMAGE_FORMAT_JPEG, false, function () {
            texture.release();
            jsb.reflection.callStaticMethod(
                packageUri + '/utils/QQUtil',
                'sharePic',
                '(Ljava/lang/String;Z)V',
                nameJPG,
                false
            );
        });
    }
};

/**
 * 分享url到qq
 * @param {String} url 地址
 * @param {String} title 标题
 * @param {String} description 内容
 * @param {Boolean || Number} sceneType 分享类型(false或0: qq, true或其他数字: 空间)
 * @param {String} transaction 单号(时间戳)
 */
QQUtil.shareUrl = function (url, title, description, sceneType, transaction) {
    cc.log('QQUtil.shareUrl');
    if (cc.sys.os == cc.sys.OS_IOS) {
        jsb.reflection.callStaticMethod(
            'QQUtil',
            'shareUrl:title:description:sceneType:',
            url,
            title,
            description,
            (sceneType ? 1 : 0)
        );
    } else if (cc.sys.os == cc.sys.OS_ANDROID) {
        jsb.reflection.callStaticMethod(
            packageUri + '/utils/QQUtil',
            'shareUrl',
            '(Ljava/lang/String;Ljava/lang/String;Ljava/lang/String;ZLjava/lang/String;)V',
            url,
            title,
            description,
            !!sceneType,
            transaction
        );
    }
};

/**
 * 分享文本到qq
 * @param {String} text 文本内容
 * @param {String} [sceneType] 0
 * @param {String} [transaction] 0
 */
QQUtil.shareText = function (text, sceneType, transaction) {
    if (cc.sys.os == cc.sys.OS_IOS) {
        jsb.reflection.callStaticMethod(
            'QQUtil',
            'shareText:',
            text
        );
    } else if (cc.sys.os == cc.sys.OS_ANDROID) {
        jsb.reflection.callStaticMethod(packageUri + '/utils/QQUtil',
            'shareText',
            '(Ljava/lang/String;)V',
            text
        );
    }
};